package com.bw.z.mvp.view.activity;

import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.bw.z.R;
import com.bw.z.db.DBManager;
import com.bw.z.db.UserTable;
import com.bw.z.db.UserTableDao;
import com.bw.z.mvp.base.view.BaseActivity;
import com.bw.z.mvp.contract.RegisterContract;
import com.bw.z.mvp.di.DaggerRegisterComponent;
import com.bw.z.mvp.di.RegisterModules;
import com.bw.z.mvp.model.entity.RegisterEntity;
import com.bw.z.mvp.presenter.RegisterPresenter;
import com.google.gson.Gson;

import java.util.List;

public class RegisterActivity extends BaseActivity<RegisterPresenter>
        implements RegisterContract.registerView, View.OnClickListener {

    private android.widget.ImageView registerBack;
    private android.widget.EditText registerName;
    private android.widget.EditText registerPwd;
    private android.widget.Button registerBtn;
    private DBManager dbManager;

    @Override
    public View addStatusView() {
        return f(R.id.register_linear);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        getColors(Color.WHITE);
        registerBack = (ImageView) findViewById(R.id.register_back);
        registerName = (EditText) findViewById(R.id.register_name);
        registerPwd = (EditText) findViewById(R.id.register_pwd);
        registerBtn = (Button) findViewById(R.id.register_btn);

        registerBack.setOnClickListener(this::onClick);
        registerBtn.setOnClickListener(this::onClick);

    }

    @Override
    public void inject() {
        DaggerRegisterComponent.builder().registerModules(new RegisterModules(this)).build().injectRegister(this);
    }

    @Override
    public void initData() {
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.register_back) {
            finish();
        } else {
            String name = registerName.getText().toString().trim();
            LogUtils.e("name:" + name);
            String pwd = registerPwd.getText().toString().trim();
            if (name.equals("") || pwd.equals("")) {
                Toast.makeText(RegisterActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
            } else {
                int i = Integer.parseInt(name);
                LogUtils.e("i:" + i);
                p.getRegisterData(i, pwd);
            }
        }
    }

    @Override
    public void requestRegister(RegisterEntity registerEntity) {
        LogUtils.json(registerEntity);
        String values = registerEntity.getValues();
        if (registerEntity.status.equals("200")) {
            LogUtils.e(registerEntity.message);
            dbManager = new DBManager(RegisterActivity.this);
            UserTable user = new Gson().fromJson(values, UserTable.class);
            UserTableDao userTableDao = dbManager.getDaoSession().getUserTableDao();
            List<UserTable> userTables = userTableDao.loadAll();
            boolean isCheck = false;

            for (int i = 0; i < userTables.size(); i++) {
                if (userTables.get(i).getUser_phone() == user.getUser_phone()) {
                    isCheck = true;
                    break;
                }
            }
            if (!isCheck) {
                userTableDao.insert(user);
            }
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dbManager = null;
    }


}